package cn.com.aiouyi.service.impl;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.subject.Subject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.com.aiouyi.dao.MemberDao;
import cn.com.aiouyi.entity.Member;
import cn.com.aiouyi.page.Filter;
import cn.com.aiouyi.service.MemberService;
import cn.com.aiouyi.shiro.Principal;

/**
 * Service - 用户
 * @author hanson
 *
 */
@Transactional
@Service("memberServiceImpl")
public class MemberServiceImpl extends BaseServiceImpl<Member, Long> implements MemberService{

	@Override
	public Member findByUsername(String username) {
		if(StringUtils.isBlank(username)){
			return null;
		}
		List<Filter> filters = new ArrayList<Filter>();
		filters.add(Filter.eq("username", username));
		return this.findOne(filters);
	}
	
	@Resource(name = "memberDaoImpl")
	private MemberDao memberDao;
	
	@Transactional(readOnly = true)
	public Member getCurrent() {
		Subject subject = SecurityUtils.getSubject();
		if (subject != null) {
			Principal principal = (Principal) subject.getPrincipal();
			if (principal != null) {
				return memberDao.find(principal.getId());
			}
		}
		return null;
	}

}
